import Koa from 'koa';
import Router from 'koa-router';
import bodyParser from 'koa-bodyparser';
import cors from 'koa-cors';
import logger from 'koa-logger';
import dotenv from 'dotenv';
import { deepseekRouter } from './routes/deepseek';
import { errorHandler } from './middleware/errorHandler';

// 加载环境变量
const result = dotenv.config();

if (result.error) {
  console.error('Error loading .env file:', result.error);
} else {
  console.log('.env file loaded successfully');
  console.log('DB_HOST:', process.env.DB_HOST); // 示例变量
}

const app = new Koa();
const router = new Router();

// 中间件
app.use(errorHandler);
app.use(logger());
app.use(cors());
app.use(bodyParser());

// 路由
app.use(router.routes()).use(router.allowedMethods());
app.use(deepseekRouter.routes()).use(deepseekRouter.allowedMethods());

// 健康检查接口
router.get('/health', (ctx) => {
  ctx.body = {
    status: 'ok',
    timestamp: new Date().toISOString(),
    message: 'Koa DeepSeek API Server is running'
  };
});

const PORT = 16066

app.listen(PORT, () => {
  console.log(`🚀 服务器已启动，监听端口: ${PORT}`);
  console.log(`📡 健康检查: http://localhost:${PORT}/health`);
  console.log(`🤖 DeepSeek API: http://localhost:${PORT}/api/deepseek/chat`);
}); 